<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta charset="utf-8" />
	<title></title>
	<link href="../css/bootstrap.min.css" rel="stylesheet">
	<link href="../css/bootstrap-select.css" rel="stylesheet">
	<link href="../css/font-awesome.min.css" rel="stylesheet">
	<link href="../css/JyyCommon.css" rel="stylesheet">
	<link href="../css/JyyTable.css" rel="stylesheet">
	<link href="../css/JyyTree.css" rel="stylesheet">
	<link href="../css/JyyMsgBox.css" rel="stylesheet">
	<script type="text/javascript" src="../js/jquery-3.2.1.js"></script>
	<script type="text/javascript" src="../js/jquery.form.min.js"></script> 
	<script type="text/javascript" src="../js/bootstrap.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-paginator.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-select.js"></script>
	<script type="text/javascript" src="../js/JyyCommon.js"></script>
	<script type="text/javascript" src="../js/JyyTable.js"></script>
	<script type="text/javascript" src="../js/JyyTree.js"></script>
	<script type="text/javascript" src="../js/JyyMsgBox.js"></script>
	<script type="text/javascript" src="../js/JyyLoading.js"></script>
	<style>
	.heidenbt{
		display: none
	}
</style>
	<script type="text/javascript">
		var tree = null;
		var table = null;
		var scheme = null;
		var treeCode = null;
		$(function(){
			//scheme = parent.getSelectScheme();
			//加载树组件
			var treeData = getTreeData();
			tree = RxTree.init("tree", treeData, {listMode:"tree",draggable:true,checkType:"single", drillType:"1"}, {width:"200px", bottom:"0px", top:"0px", border:"none",borderRight: "1px solid #e0e0e0"}, onTreeEvent);
			tree.expandAll();
			tree.checkNode("101", true);
			$("#tree").css("top","40px");
		})
		
		function onTreeEvent(event){
			console.log("onTreeEvent");
			if(event.name == "onclick"){
				var data = event.node.data;
				if(data.isLeaf){
					if(data.code == "11"){
						$("#allDownload").click();
					}else if(data.code == "12"){
						//再次修改input内容
						var form = document.getElementById('form2');
						$(form).find("input").val(parent.planId);
						form.submit();
					}else if (data.code == "901"){
						$('#sampleIframe').attr('src','../g/pages/sample.html?projectId='+parent.planId);
						$('#sampleIframe').attr('src', $('#sampleIframe').attr('src'));
						$("#table").css("display","none");
						$("#operate").css("display","none");
						$("#iframe").css("display","block");
					}else{
						$("#table").css("display","block");
						$("#operate").css("display","block");
						$("#iframe").css("display","none");
						treeCode = data.code;
						setlabel(data.code);
						JyyLoading.start();
						$.when(getTableData(data.code)).done(function(tableData){
							if(table == null){
								table = JyyTable.init("table", tableData, {position:"absolute",top:"50px",left:"205px",right:"5px",bottom:"0px",overflow:"hidden"}, onTableEvent);
							}else{
								table.refresh(tableData);
							}
							JyyLoading.end();
						});
					}
				}
			}
		}
		function setlabel(code){
			var label = "";
			$("#daochu").removeClass("heidenbt")
			$("#daoru").removeClass("heidenbt")
			$("#xianshi").removeClass("heidenbt")
			$("#qingkong").addClass("heidenbt")
			
			if(code == "101"){
				label = "能量枢纽名称";
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
			}else if(code == "102"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "元件名称";
			}else if(code == "103"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "支路名称";
			}else if(code == "104"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "元件名称";
			}else if(code == "105"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "元件类型名称";
			}else if(code == "106"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "元件名称";
			}else if(code == "107"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "元件名称";
			}else if(code == "201"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "电源名称";
			}else if(code == "202"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "节点名称";
			}else if(code == "203"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "线路名称";
			}else if(code == "204"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "节点名称";
			}else if(code == "205"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "元件名称";				
			}else if(code == "206"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "元件名称";
			}else if(code == "207"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "电源名称";
			}else if(code == "301"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "气源名称";
			}else if(code == "302"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "节点名称";
			}else if(code == "303"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "线路名称";
			}else if(code == "304"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "节点名称";
			}else if(code == "305"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "元件名称";
			}else if(code == "306"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "气源名称";
			}else if(code == "401"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "热源名称";
			}else if(code == "402"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "节点名称";
			}else if(code == "403"){
				$("#daochu").addClass("heidenbt")
				$("#daoru").addClass("heidenbt")
				$("#xianshi").addClass("heidenbt")
				label = "线路名称";
			}else if(code == "404"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "节点名称";
			}else if(code == "405"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "元件名称";
			}else if(code == "406"){
				$("#xianshi").addClass("heidenbt")
				$("#qingkong").removeClass("heidenbt")
				label = "热源名称";
			}
			$("#searchName").text(label);
		}
		
		function onTableEvent(type, value){
			console.log("onTableEvent");
			var code = tree.getSelectData().code;
			if(type == "insertBefore"){
				console.log("insertBefore");
				//核查数据正确性
				return true;
			} else if(type == "data"){
				JyyLoading.start();
 				$.ajax({
		            type:"POST",
		            url:"../pv/page/checkToMeos/"+code,
		            data:{"value":JSON.stringify(value),"projectid":parent.planId},
		      //      contentType:"application/json", 
		            success:function(data){
		            	var pageSize = table.config.page.option.pageSize;
						$.when(getTableData(code, 1, pageSize, $("#term").val())).done(function(tableData){
							table.refresh(tableData);
							JyyLoading.end();
						});
		            }
		        });
			} else if(type == "page"){
				console.log("page");
				var val = $("#term").val();
				var code = tree.getSelectData().code;
				JyyLoading.start();
				$.when(getTableData(code, value.currentPage, value.pageSize, val)).done(function(tableData){
					table.refresh(tableData);
					JyyLoading.end();
				});
			}
		}
		//修改按钮
		function updateRow(){
			table.updateRows();
		}
		//取消修改按钮
		function cancelRow(){
			table.cancelRow();
		}
		//保存按钮
		function saveRow(){
			table.saveRow();
		}
		//下载按钮
		function download(){
			var form = $('<form action="../pv/pageExcel/downloadMeosExcel/'+ treeCode +'" style="display:none" method="post">');
			var input = $('<input type="hidden" name="projectid">');
		//	var iframe = $('<iframe name="downloadIframe" type="hidden">');
			input.attr("value",parent.planId);
			form.append(input);
		//	iframe.contents().find('body').append(form)
			$("body").append(form);
			form.submit();
			//iframe.remove();
			form.remove();
		}
		
		//上传按钮
		function upload(){
			var file = document.getElementById("file");
			if(file.value == ""){
				return;
			}
			JyyLoading.start();
			var fileObj = file.files[0];
			var url = "../pv/pageExcel/loadMeosExcel/"+treeCode;
			var form = new FormData();
			form.append("file",fileObj);
			form.append("schemeid",parent.planId);
			var xhr = new XMLHttpRequest();
			xhr.open("post", url, true);
			xhr.send(form);
			xhr.onreadystatechange = function(){
			    if(xhr.readyState === 4 && xhr.status === 200){
					$.when(getTableData(treeCode)).done(function(tableData){
						table.refresh(tableData);
						JyyLoading.end();
						document.getElementById("file").value=null
					});
			    }
			}

		}

		//全量上传事件
		function allDownloads(event){
			if(event.value == ""){
				return;
			}
			JyyLoading.start();
			$("#form1").find("input[name='schemeid']").val(parent.planId);
			$("#form1").ajaxSubmit({
				success : function(data) {
					alert(data);
					location.reload();
				}
			});
		}
		
		//显示隐藏列按钮
		function colsState(e){
			var i = $(e).find('i');
			if($(i).is('.glyphicon-align-justify')){
				$(i).removeClass('glyphicon-align-justify')
				$(i).addClass('glyphicon-list');
				$(e).html($(e).html().replace("显示隐藏列", "隐藏默认列"));
				table.hideCols(false);
			}else{
				$(i).removeClass('glyphicon-list')
				$(i).addClass('glyphicon-align-justify');
				$(e).html($(e).html().replace("隐藏默认列", "显示隐藏列"));
				table.hideCols(true);
			}
		}
		//查询
		function select(){
			var value = $("#term").val();
			var code = tree.getSelectData().code;
			//var currentPage = table.config.page.option.currentPage;
			var currentPage = 1;
			var pageSize = table.config.page.option.pageSize;
			JyyLoading.start();
			$.when(getTableData(code, currentPage, pageSize, value)).done(function(tableData){
				table.refresh(tableData);
				JyyLoading.end();
			});
		}
		//重置
		function reset(){
			$("#term").val("");
		//	var currentPage = table.config.page.option.currentPage;
			var currentPage = 1;
			var pageSize = table.config.page.option.pageSize;
			
			JyyLoading.start();
			$.when(getTableData(treeCode, currentPage, pageSize, null)).done(function(tableData){
				table.refresh(tableData);
				JyyLoading.end();
			});
		}
		
		function getTreeData(){
			return [{	
					code:"10",name:"批量处理",draggable:false,level:1,
					items:[ {code:"11",name:"全量导入",isLeaf:true,draggable:false,items:[]},
							{code:"12",name:"全量导出",isLeaf:true,draggable:false,items:[]}]
				},{	code:"100",name:"能量枢纽数据",draggable:false,level:1,
					items:[	{code:"101",name:"能量枢纽",isLeaf:true,draggable:false,items:[]},
							{code:"102",name:"能量枢纽节点",isLeaf:true,draggable:false,items:[]},
							{code:"103",name:"能量枢纽支路",isLeaf:true,draggable:false,items:[]},
							{code:"104",name:"能量枢纽设备元件",isLeaf:true,draggable:false,items:[]},
							{code:"105",name:"元件类型",isLeaf:true,draggable:false,items:[]},
							{code:"106",name:"能量枢纽负荷",isLeaf:true,draggable:false,items:[]},
							{code:"107",name:"分布式新能源",isLeaf:true,draggable:false,items:[]}]
				},{
					code:"200",name:"电网数据",draggable:false,level:1,
					items:[	{code:"201",name:"电网电源",isLeaf:true,draggable:false,items:[]},
							{code:"202",name:"电网节点",isLeaf:true,draggable:false,items:[]},
							{code:"203",name:"电网线路",isLeaf:true,draggable:false,items:[]},
							{code:"204",name:"电负荷",isLeaf:true,draggable:false,items:[]},
							{code:"205",name:"集中式新能源",isLeaf:true,draggable:false,items:[]},
							{code:"206",name:"电源价格",isLeaf:true,draggable:false,items:[]},
							{code:"207",name:"电源时序碳排系数",isLeaf:true,draggable:false,items:[]}]
				},{
					code:"300",name:"气网数据",draggable:false,level:1,
					items:[	{code:"301",name:"气网气源",isLeaf:true,draggable:false,items:[]},
							{code:"302",name:"气网节点",isLeaf:true,draggable:false,items:[]},
							{code:"303",name:"气网线路",isLeaf:true,draggable:false,items:[]},
							{code:"304",name:"气负荷",isLeaf:true,draggable:false,items:[]},
							{code:"305",name:"气源价格",isLeaf:true,draggable:false,items:[]},
							{code:"306",name:"气源时序碳排系数",isLeaf:true,draggable:false,items:[]}]
				},{
					code:"400",name:"热网数据",draggable:false,level:1,
					items:[	{code:"401",name:"热网热源",isLeaf:true,draggable:false,items:[]},
							{code:"402",name:"热网节点",isLeaf:true,draggable:false,items:[]},
							{code:"403",name:"热网线路",isLeaf:true,draggable:false,items:[]},
							{code:"404",name:"热负荷",isLeaf:true,draggable:false,items:[]},
							{code:"405",name:"热源价格",isLeaf:true,draggable:false,items:[]},
							{code:"406",name:"热源时序碳排系数",isLeaf:true,draggable:false,items:[]}]
				}
			];
		}
		function getTableData(code, currentPage, pageSize, keyword){
			var defer = $.Deferred();
			var result = null;
			var projectId = sessionStorage.getItem("projectId");
			$.ajax({
				dataType: "json",
				async:true,
				data:{"pagenow":currentPage,"pagenum":pageSize,"keyword":keyword,"projectid":parent.planId},
				url: "../pv/page/pageInfoToMeos/"+code
			}).done(function(res) {
				var sts=eval("("+res+")");
				result = sts;
				defer.resolve(sts);
			});
			return defer.promise();
			$("#file").value=null
		}
		function deleteAll() {
			JyyMsgBox.confirm({ title:"提示", message:"确定要清空数据吗?",type:"warning"}).on(function (e) {
				if (e) {
					$.ajax({
			        	type:"POST",
			            url:"../pv/page/meosDeleteAll",
			            data:{"projectid":parent.planId, "code":treeCode},
			            success:function(data){
			            	var pageSize = table.config.page.option.pageSize;
							$.when(getTableData(treeCode, 1, pageSize, $("#term").val())).done(function(tableData){
								table.refresh(tableData);
								JyyLoading.end();
							});
			            }
			        });
				}
			});
		}
	</script>
</head>
<body style="position:absolute;top:0px;left:0px;right:0px;bottom:0px;">
	<div id="operate" style="position: absolute;top: 0px;left: 200px;right: 0px;height: 50px;">
		<div class="col-md-12" style="position: absolute;top: 10px;padding-left: 5px;padding-right: 5px;">
			
			<button class="btn btn-info btn-sm" onclick=updateRow()><i class="glyphicon glyphicon-pencil"></i>修改</button>
			<button class="btn btn-info btn-sm" onclick=cancelRow()><i class="glyphicon glyphicon-share-alt"></i>取消修改</button>
			<button class="btn btn-info btn-sm" onclick=saveRow()><i class="glyphicon glyphicon-saved"></i>保存</button>
			<button class="btn btn-info btn-sm" id="daochu" onclick=download()><i class="glyphicon glyphicon-download"></i>导出Excel</button>
			<button class="btn btn-info btn-sm" id="daoru" onclick=$("#file").click();><i class="glyphicon glyphicon-upload"></i>导入Excel</button>
			<button class="btn btn-info btn-sm" id ="xianshi" onclick=colsState(this)><i class="glyphicon glyphicon-align-justify"></i>显示隐藏列</button>
			<button class="btn btn-info btn-sm" id="qingkong" onclick=deleteAll()><i class="glyphicon glyphicon-remove"></i>清空数据</button>
			
			<button class="btn btn-info btn-sm" style="float: right;" onclick=reset()>重置</button>
			<button class="btn btn-info btn-sm" style="float: right;margin-right: 5px;" onclick=select()>查询</button>

			<div class="col-sm-3" style="float: right;width:170px;padding-left: 0px;">
            	<input id="term" type="text" class="form-control"style="width:150px;height:30px;font-size: 12px;" id="txt_search_departmentname">
            </div>
			<label id="searchName" class="control-label col-sm-1" style="float: right;margin-top: 7px;padding: 0px;width: 80px;font-weight: normal;" for="txt_search_departmentname"></label>
		
			<input type="file" id="file" name="file" onchange="upload(this)" style="display:none"/>
		</div>
	</div>
	<div id="tree"></div>
	<div id="table"></div>
	<div id="iframe" style="position: absolute;top: 0px;left: 200px;right: 0px;height: 820px;display:none">
		<iframe src="../g/pages/sample.html?projectId=77" frameborder="0" width="100%" scrolling="No" height="100%" leftmargin="0" topmargin="0" id="sampleIframe"></iframe>
	</div>
	<form action='../pv/page/meosInfo/11' method='post' id='form1' enctype="multipart/form-data">
		<input type="file" id="allDownload" name="allDownload" onchange="allDownloads(this)" style="display:none"/>
		<input type="hidden" name="schemeid">
	</form>
	<form action='../pv/page/meosInfo/12' method='post' id='form2'>
		<input type="hidden" name="schemeid">
	</form>
</body>
</html>